export function buildFrameTree(docTree) {
    const doc = {
        children: parseFramesFromDoc(docTree),
    };
    return doc;
}

function parseFramesFromDoc(docTree) {
    const elements = docTree.children ? [...docTree.children] : [];
    const frames = [];

    while (elements.length) {
        const elem = elements.pop();
        if (elem.nodeName === 'IFRAME') {
            frames.push(elem);
        } else if (elem.children) {
            elements.push(...elem.children);
        }
    }

    return frames
        .map((frame) => ({
            nodeId: frame.nodeId,
            backendNodeId: frame.backendNodeId,
            parentId: frame.parentId,
            frameId: frame.frameId,
            documentURL: getUrlForFrame(frame),
            documentNodeId: getDocumentNodeIdForFrame(frame),
            children: frame.contentDocument ? parseFramesFromDoc(frame.contentDocument) : [],
        }))
        .sort((frm1, frm2) => frm1.nodeId - frm2.nodeId);
}

function getUrlForFrame(frame) {
    const srcAttrIndex = frame.attributes.indexOf('src');
    return srcAttrIndex !== -1 ? frame.attributes[srcAttrIndex + 1] : undefined;
}

function getDocumentNodeIdForFrame(frame) {
    try {
        return frame.contentDocument.children.find(
            (child) => child.children && child.children.length && child.children.length > 1 && child.children[1].nodeName === 'BODY',
        ).children[1].nodeId;
    } catch (e) {
        return undefined;
    }
}
